package com.turtledove.storyalbum.control;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.turtledove.storyalbum.R;
import com.turtledove.storyalbum.common.ConstCommon;
import com.turtledove.storyalbum.model.adapter.AdapterAlbumAddNewImage;
import com.turtledove.storyalbum.model.helper.DBHandler;
import com.turtledove.storyalbum.model.object.ObjectImage;

public class AlbumAddNewImageActivity extends Activity implements OnClickListener {

	GridView listImage;
	AdapterAlbumAddNewImage adapter;
	TextView txtItemSelected, txtDone;
	ArrayList<ObjectImage> arrayImage, arrayImageSelected;
	Dialog dialog;
	Context mContext;
	String mResultIdAlbum;
	
	
	String KEY_FROM_WHERE;
	int KEY_ID_ALBUM;

	// ____________________ System Function _____________________________

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_gallery);
		mContext = this;
		initDB();
		initWidget();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.txtDone:
			if(KEY_FROM_WHERE.equals(ConstCommon.KEY_FROM_WHERE_ALBUM))
			{
				showDialogAlbum();
			}else if(KEY_FROM_WHERE.equals(ConstCommon.KEY_FROM_WHERE_ITEM))
			{
				updateListImage(KEY_ID_ALBUM);
			}

			break;

		default:
			break;
		}

	}

	// ________________________ Custom Function ______________

	private void initWidget() {
		listImage = (GridView) findViewById(R.id.listImage);
		listImage.setFastScrollEnabled(true);
		txtItemSelected = (TextView) findViewById(R.id.txtItemSelected);
		txtItemSelected.setOnClickListener(this);
		txtDone = (TextView) findViewById(R.id.txtDone);
		txtDone.setOnClickListener(this);
		arrayImage = new ArrayList<ObjectImage>();
		arrayImage = ConstCommon.listImageFromFolder;
		adapter = new AdapterAlbumAddNewImage(this, arrayImage);
		listImage.setAdapter(adapter);
	}

	private void initDB()
	{
		Intent intent = new Intent();
		intent = getIntent();
		String keyFromAlbum = intent.getExtras().getString(ConstCommon.KEY_FROM_WHERE_ALBUM);
		String keyFromItem = intent.getExtras().getString(ConstCommon.KEY_FROM_WHERE_ITEM);
		if(keyFromAlbum != null && keyFromAlbum.equals(ConstCommon.KEY_FROM_WHERE_ALBUM))
		{
			KEY_FROM_WHERE = ConstCommon.KEY_FROM_WHERE_ALBUM;
		}else if(keyFromItem != null && keyFromItem.equals(ConstCommon.KEY_FROM_WHERE_ITEM))
		{
			KEY_FROM_WHERE = ConstCommon.KEY_FROM_WHERE_ITEM;
			KEY_ID_ALBUM = intent.getExtras().getInt(ConstCommon.KEY_FROM_WHERE_ITEM_ID_ALBUM);
		}
	}
	
	public ArrayList<ObjectImage> getListImageSelected() {
		ArrayList<ObjectImage> listImageSelected = new ArrayList<ObjectImage>();
		try {
			if (arrayImage.size() > 0) {
				for (int i = 0; i < arrayImage.size(); i++) {
					if (arrayImage.get(i).getIsSelected()) {
						listImageSelected.add(arrayImage.get(i));
					}
				}
			}
			txtItemSelected.setText(listImageSelected.size() + " "
					+ getResources().getString(R.string.selected));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listImageSelected;
	}

	void showDialogAlbum() {
		try {
			dialog = new Dialog(this);
			dialog.setTitle(getResources().getString(R.string.change_album));
			dialog.setContentView(R.layout.dialog_album_info);
			final EditText dialogNameAlbum = (EditText) dialog
					.findViewById(R.id.dialogNameAlbum);
			final CheckBox cbCurrentAlbum = (CheckBox) dialog
					.findViewById(R.id.cbCurrentAlbum);
			Button dialog_BtnYes = (Button) dialog.findViewById(R.id.dialog_BtnYes);
			Button dialog_BtnNo = (Button) dialog.findViewById(R.id.dialog_BtnNo);
			dialog_BtnYes.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					String currentAlbum = "0";
					if (cbCurrentAlbum.isChecked()) {
						currentAlbum = "1";
						DBHandler.disableCurrentAlbum(mContext);
					}
					// insert album
					DBHandler.insertAlbum(mContext, dialogNameAlbum.getText().toString().trim(), currentAlbum);
					// insert item of album
					int maxIdAlbum = DBHandler.getMaxIdAlbum(mContext);
					if (maxIdAlbum > 0) {
						mResultIdAlbum = String.valueOf(maxIdAlbum);
						arrayImageSelected = new ArrayList<ObjectImage>();
						arrayImageSelected = getListImageSelected();
						if (arrayImageSelected.size() > 0) {
							for (int i = 0; i < arrayImageSelected.size(); i++) {
								String itemCover = "0";
								if (i == 0) {
									itemCover = "1";
								}
								DBHandler.insertItem(mContext, arrayImageSelected
										.get(i).getUrlImage(), String
										.valueOf(maxIdAlbum), itemCover);
							}
						}
					}
					//change status service widget
					WidgetUpdateService.statusCurrentAlbum = true;
					
					dialog.dismiss();
					Intent returnIntent = new Intent();
					returnIntent.putExtra(ConstCommon.KEY_RESULT_ID_ALBUM, mResultIdAlbum);
					setResult(RESULT_OK, returnIntent);
					finish();
				}
			});
			dialog_BtnNo.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			dialog.show();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	void updateListImage(int idAlbum)
	{
		try {
			arrayImageSelected = new ArrayList<ObjectImage>();
			arrayImageSelected = getListImageSelected();
			if (arrayImageSelected.size() > 0) {
				for (int i = 0; i < arrayImageSelected.size(); i++) {
					String itemCover = "0";
					DBHandler.insertItem(mContext, arrayImageSelected
							.get(i).getUrlImage(), String
							.valueOf(idAlbum), itemCover);
				}
			}
			Intent returnIntent = new Intent();
			returnIntent.putExtra(ConstCommon.KEY_RESULT_ID_ALBUM, idAlbum);
			setResult(RESULT_OK, returnIntent);
			finish();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
